package cn.com.tianpeidong.restaurantadmin.project.admin.role.rest;

import cn.com.tianpeidong.restaurantadmin.common.util.page.PageWrapper;
import cn.com.tianpeidong.restaurantadmin.project.admin.role.entity.Role;
import cn.com.tianpeidong.restaurantadmin.project.admin.role.service.IRoleService;
import cn.com.tianpeidong.restaurantadmin.project.admin.role.vo.OperateRole;
import cn.com.tianpeidong.restaurantadmin.project.admin.role.vo.RoleVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import javax.validation.Valid;
import java.net.URI;
import java.util.List;

/**
 * @author tianpeidong
 * @version 创建时间: 19-4-7 下午6:20
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @GetMapping
    @RequiresPermissions("role:list")
    public ResponseEntity list(
            @RequestParam(value = "per_page", required = false) Integer pageSize,
            @RequestParam(value = "pageSize", required = false) Integer page,
            @RequestParam("pagi") Boolean isPagi) {
        if (!isPagi) {
            return this.getAllRoles();
        }
        PageWrapper pageWrapper = new PageWrapper(pageSize, page);
        List<RoleVO> list = roleService.listRole(new RoleVO());
        return ResponseEntity.ok(pageWrapper.getResult(list));
    }

    @PostMapping("/search")
    @RequiresPermissions("role:list")
    public ResponseEntity search(
            @RequestParam(value = "per_page", required = false) Integer pageSize,
            @RequestParam(value = "pageSize", required = false) Integer page,
            @RequestBody RoleVO roleVo) {
        PageWrapper pageWrapper = new PageWrapper(pageSize, page);
        List<RoleVO> list = roleService.listRole(roleVo);
        return ResponseEntity.ok(pageWrapper.getResult(list));
    }

    @GetMapping("/{id}")
    @RequiresPermissions("role:list")
    public ResponseEntity getRoleById(@PathVariable("id") Long id) {
        Role roleInfo = roleService.getRoleById(id);
        return ResponseEntity.ok(roleInfo);
    }

    @PostMapping
    @RequiresPermissions("role:add")
    public ResponseEntity add(@Valid @RequestBody OperateRole roleVo) {
        Role newRole = roleService.add(roleVo);
        URI location = ServletUriComponentsBuilder.fromCurrentRequest()
                .path("/{id}")
                .buildAndExpand(newRole.getId())
                .toUri();
        return ResponseEntity.created(location).build();
    }

    @PutMapping
    @RequiresPermissions("role:edit")
    public ResponseEntity edit(@Valid @RequestBody OperateRole roleVo) {
        Role editRole = roleService.edit(roleVo);
        URI location = ServletUriComponentsBuilder.fromCurrentRequest()
                .path("/{id}")
                .buildAndExpand(editRole.getId())
                .toUri();
        return ResponseEntity.created(location).build();
    }

    @DeleteMapping("/{roleId}")
    @RequiresPermissions("role:delete")
    public ResponseEntity delete(@PathVariable("roleId") Long roleId) {
        roleService.delete(roleId);
        return ResponseEntity.noContent().build();
    }


    private ResponseEntity getAllRoles() {
        return ResponseEntity.ok(roleService.listRole(new RoleVO()));
    }
}
